//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
var zhenzisms = require("../../utils/zhenzisms.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 省市区
    region: ['', '', ''],
    codeValue: "获取验证码",
    flag: false,
    phone: "",
    hidden: true,
    code: '',
    second: 1,
    timer: null
  },

 

  onShow: function () {
    console.log("0000");
  },
  // 输入手机号
  phoneTap: function (e) {
    console.log(e);
    this.setData({
      phone: e.detail.value,
    });
    if (e.detail.value != '') {
      this.setData({
        hidden: false,
        codeValue: '获取验证码'
      })
    } else {
      this.setData({
        hidden: true
      })
    }
  },
  // 输入验证码
  bindCodeInput(e) {
    this.setData({
      code: e.detail.value
    })
    console.log(e);
  },

  // 获取验证码
  getCode: function () {
    clearInterval(timer)
    var that = this;
    var phone = this.data.phone;
    var codeValue = this.data.codeValue;
    var myreg = /^1[3456789]\d{9}$/;
    if (!myreg.test(phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      // return false;
    } else {
      var second = 60
      var timer = that.data.timer
      that.setData({
        codeValue: second + "s",
        flag: true,
        timer: 0
      })
      wx.request({
        url: baseJs.api_Utils + "CreateVerificationCode" + "?" + "MobilePhone=" + phone + "&t=" + "1",
        success: function (res) {
          var json = res.data;
          console.log(json)
          if (json.ResultFlag) {
            wx.showToast({
              title: '验证码已发送',
              icon: "none"
            })
            timer = setInterval(function () {
              second -= 1;
              if (second > 0) {
                that.setData({
                  codeValue: second + "s",
                  flag: true,
                  timer: 0
                })
                clearInterval(that.data.timer)
              } else if (second < 0) {
                clearInterval(timer)
              } else {
                clearInterval(timer);
                that.setData({
                  codeValue: "重新获取",
                  flag: false,
                  timer: 0
                })
                console.log(second);
                clearInterval(that.data.timer)
              }
            }, 1000);
          } else {
            wx.showToast({
              title: json.ResultMsg,
              icon: "none"
            })
          }
        }
      })
    }
  },
  // 省市区
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  // 确认注册
  formSubmit: function (e) {
    console.log(e);
    var that = this;
    if (e.detail.value.password != e.detail.value.surePassword) {
      wx.showToast({
        title: "两次密码不一致",
        icon: 'none',
        duration: 2000
      });
      return;
    }
    wx.request({
      url: baseJs.api_User + "NewCreateUser",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'post',
      data: baseJs.json2Form({
        UserName: e.detail.value.phone,
        Password: e.detail.value.password,
        Verification: e.detail.value.code,
        InviteCode: e.detail.value.inviteCode
      }),
      success: function (res) {
        var json = res.data;
        if (json.ResultFlag == 1) {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          setTimeout(function () {
            wx.navigateTo({
              url: '/packageA/login/login',
            })
          }, 2000)
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })

  },

})